package cn.jekun.web.shop.action;

import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.utils.EncryptUtil;
import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.component.core.bean.gen.ErpShop;
import cn.jekun.component.core.bean.gen.RsUser;
import cn.jekun.component.core.bean.gen.TsAccount;
import cn.jekun.component.core.service.AccountPayService;
import cn.jekun.component.core.service.ScmService;
import cn.jekun.component.core.service.gen.ErpShopService;
import cn.jekun.component.core.service.gen.RsUserService;
import cn.jekun.component.core.service.gen.TsAccountService;

/**
 * 安全设置
 * 
 * @author twj
 * 
 */
public class SafeSetAction extends ShopBaseAction {
	private static final String AUTH_SHOP_MY_SECURITY = "SHOP_MY_SECURITY";// 安全设置
	protected final Log log = LogFactory.getLog(SafeSetAction.class);
	private String shopName;
	private String oldMobile;
	private String newMobile;
	private String oldLoginPwd;// 旧登录密码
	private String newLoginPwd;// 新登录密码
	private String oldPayPwd;// 旧支付密码
	private String newPayPwd;// 新支付密码
	@Resource(name = RsUserService.BEAN_NAME)
	RsUserService userService;
	
	@Resource(name = ErpShopService.BEAN_NAME)
	ErpShopService shopService;

	@Resource(name = TsAccountService.BEAN_NAME)
	TsAccountService accountService;
	
	@Resource(name = AccountPayService.BEAN_NAME)
	AccountPayService accountPayService;
	
	@Resource(name = RsUserService.BEAN_NAME)
	RsUserService rsUserService;
	
	@Resource(name = ScmService.BEAN_NAME)
	ScmService scmService;

	public String getShopName() {
		return shopName;
	}

	public void setShopName(String shopName) {
		this.shopName = shopName;
	}

	public String getOldLoginPwd() {
		return oldLoginPwd;
	}

	public void setOldLoginPwd(String oldLoginPwd) {
		this.oldLoginPwd = oldLoginPwd;
	}

	public String getNewLoginPwd() {
		return newLoginPwd;
	}

	public void setNewLoginPwd(String newLoginPwd) {
		this.newLoginPwd = newLoginPwd;
	}

	public String getOldPayPwd() {
		return oldPayPwd;
	}

	public void setOldPayPwd(String oldPayPwd) {
		this.oldPayPwd = oldPayPwd;
	}

	public String getNewPayPwd() {
		return newPayPwd;
	}

	public void setNewPayPwd(String newPayPwd) {
		this.newPayPwd = newPayPwd;
	}

	public String getOldMobile() {
		return oldMobile;
	}

	public void setOldMobile(String oldMobile) {
		this.oldMobile = oldMobile;
	}

	public String getNewMobile() {
		return newMobile;
	}

	public void setNewMobile(String newMobile) {
		this.newMobile = newMobile;
	}
	
	private TsAccount tsAccount;
	

	public TsAccount getTsAccount() {
		return tsAccount;
	}

	public void setTsAccount(TsAccount tsAccount) {
		this.tsAccount = tsAccount;
	}

	/**
	 * 查询商户账号和绑定手机号码
	 * 
	 * @return
	 */
	public String queryMessage() {
		// 判断是否有操作权限
//		this.checkRight(AUTH_SHOP_MY_SECURITY);
		
		JSONObject jsonObject=new JSONObject();
		
		try {
			shopName = this.getSessionUser().getShopName();
			oldMobile = this.getSessionUser().getShopMobile();
			
			jsonObject.put("shopName", shopName);
			jsonObject.put("oldMobile", oldMobile);
			
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
			
		} catch (Exception e) {
			e.printStackTrace();
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
			
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用失败");
		}
		
		return this.writeJson(jsonObject);
		
	}

	/**
	 * 显示旧手机号
	 * 
	 * @return
	 */
	public String showMobile() {
//		oldMobile = this.getSessionUser().getShopMobile();
//		return "/shop/resetPhone.jsp";
		
		JSONObject jsonObject=new JSONObject();
		
		try {
			oldMobile = this.getSessionUser().getShopMobile();
			
			jsonObject.put("oldMobile", oldMobile);
			
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
			
		} catch (Exception e) {
			e.printStackTrace();
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
			
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用失败");
		}
		
		return this.writeJson(jsonObject);
	}

	/**
	 * 重置手机号
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String resetMobile() {
		// 检查是否存在用户
//		formMap.put(ErpShop.COL_SHOP_ID, this.getSessionUser().getShopId());
//		SqlUtis.prepareSql(formMap, SqlUtis.getSQLRequired(formMap, "a."
//				+ ErpShop.COL_SHOP_ID, Types.VARCHAR, SqlUtis.EQ));
//		boolean exists = this.shopService.exists(formMap);
//		if (!exists) {
//			this.saveActionError("用户不存在");
//			return "!/shop/SafeSet/queryMessage.do";
//		}
//		// 更新数据库
//		ErpShop shop = new ErpShop();
//		shop.setMobile(newMobile);
//		shop.setShopId(this.getSessionUser().getShopId());
//		formMap.put("erpShop", shop);
//		this.shopService.update(formMap);
//		// 记录操作日志
//		this.logService.addShopAuditLog(AUTH_SHOP_MY_SECURITY, "重置手机号");
//		// 更新session
//		this.getSessionUser().setShopMobile(newMobile);
//		this.saveActionMessage("更改手机号码成功!");
//		return "!/shop/SafeSet/queryMessage.do";
		
		JSONObject jsonObject=new JSONObject();
		
		try {
			formMap.put(ErpShop.COL_SHOP_ID, this.getSessionUser().getShopId());
			SqlUtis.prepareSql(formMap, SqlUtis.getSQLRequired(formMap, "a."
					+ ErpShop.COL_SHOP_ID, Types.VARCHAR, SqlUtis.EQ));
			boolean exists = this.shopService.exists(formMap);
			if (!exists) {
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
				jsonObject.put(ErrorMsg.KEY_MESSAGE,"用户不存在");
				return this.writeJson(jsonObject);
			}
			// 更新数据库
			ErpShop shop = new ErpShop();
			shop.setMobile(newMobile);
			shop.setShopId(this.getSessionUser().getShopId());
			formMap.put("erpShop", shop);
			this.shopService.update(formMap);
			// 记录操作日志
			this.logService.addShopAuditLog(AUTH_SHOP_MY_SECURITY, "重置手机号");
			// 更新session
			this.getSessionUser().setShopMobile(newMobile);
			
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"更改手机号码成功!");
			
		} catch (Exception e) {
			e.printStackTrace();
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
			
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用失败");
		}
		
		return this.writeJson(jsonObject);
		
	}
	/**
	 * 重置登录密码
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String resetLoginPwd() {
		
		JSONObject jsonObject=new JSONObject();
		
		try {
			// 检查是否存在用户
			formMap.put(RsUser.COL_USER_ID, this.getSessionUser().getUserId());
			SqlUtis.prepareSql(formMap, SqlUtis.getSQLRequired(formMap, "a."
					+ RsUser.COL_USER_ID, Types.VARCHAR, SqlUtis.EQ));
			boolean exists = this.userService.exists(formMap);
			if (!exists) {
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
				jsonObject.put(ErrorMsg.KEY_MESSAGE,"用户不存在！");
				return this.writeJson(jsonObject);
			}
			RsUser user = this.userService.one(formMap);
			if (!oldLoginPwd.equals(user.getPassword())) {
				jsonObject.put(ErrorMsg.KEY_CODE, 2);
				jsonObject.put(ErrorMsg.KEY_MESSAGE,"旧登录密码输入有误！");
				return this.writeJson(jsonObject);
				
			}
			user.setPassword(newLoginPwd);
			formMap.put("rsUser", user);
			this.userService.update(formMap);
			// 记录操作日志
			this.logService.addShopAuditLog(AUTH_SHOP_MY_SECURITY, "重置登录密码");
			
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"重置登录密码成功!");
			
			//同步开店用户到scm系统
//	    	RsUser tempUser = this.rsUserService.one(userId);
	    	//判断当前操作人是否为店长角色
	    	if(this.scmService.isSJ(user.getUserId())){
	    		boolean flag = this.scmService.synDataToScm(user);
	    		if(!flag){
	    			log.info("#######用户数据到SCM同步失败#########");
	    		}else{
	    			log.info("#######用户数据到SCM同步成功#########");
	    		}
	    		
	    	}
			
		} catch (Exception e) {
			e.printStackTrace();
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用失败");
		}
		return this.writeJson(jsonObject);

	}

	/**
	 * 重置支付密码
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String resetPayPwd() {
		
		JSONObject jsonObject=new JSONObject();
		
		try {
			String shopId = this.getSessionUser().getShopId();
			formMap.put(TsAccount.COL_OWNER_ID, shopId);
			formMap.put(TsAccount.COL_OWNER_TYPE_ID, "1");
			SqlUtis.prepareSql(formMap, SqlUtis.getSQL(formMap, "a."
					+ TsAccount.COL_OWNER_ID, Types.VARCHAR, SqlUtis.EQ),
			SqlUtis.getSQL(formMap,"a."+TsAccount.COL_OWNER_TYPE_ID,Types.VARCHAR,SqlUtis.EQ));
			TsAccount account = this.accountService.one(formMap);
			if(account == null){
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
				jsonObject.put(ErrorMsg.KEY_MESSAGE,"该商户暂无账户信息！");
				return this.writeJson(jsonObject);
			}else{
				
				if (!oldPayPwd.equals(account.getPassword())) {
					jsonObject.put(ErrorMsg.KEY_CODE, 2);
					jsonObject.put(ErrorMsg.KEY_MESSAGE,"旧支付密码输入有误！");
					return this.writeJson(jsonObject);
				}
				account.setPassword(newPayPwd);
				//如果是未激活的则改为激活
				if("05".equals(account.getStatusId())){
					account.setStatusId("00");
				}
				formMap.put("tsAccount", account);
				this.accountService.update(formMap);
				// 记录操作日志
				this.logService.addShopAuditLog(AUTH_SHOP_MY_SECURITY, "重置支付密码");
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
				jsonObject.put(ErrorMsg.KEY_MESSAGE,"重置支付密码成功!");
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用失败");
		}
		return this.writeJson(jsonObject);
		
		
	}
	
	/**
	 * 个人备付金账户
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String queryAccount(){
	
		
		JSONObject jsonObject=new JSONObject();
		
		try{
			
			String shopId = this.getSessionUser().getShopId();
			formMap.put(TsAccount.COL_OWNER_ID, shopId);
			formMap.put(TsAccount.COL_OWNER_TYPE_ID, "1");
			SqlUtis.prepareSql(formMap, SqlUtis.getSQL(formMap, "a."
					+ TsAccount.COL_OWNER_ID, Types.VARCHAR, SqlUtis.EQ),
			SqlUtis.getSQL(formMap,"a."+TsAccount.COL_OWNER_TYPE_ID,Types.VARCHAR,SqlUtis.EQ));
			TsAccount account = this.accountService.one(formMap);
			
			if(account != null && (account.getStatusId().equals("05") || account.getStatusId().equals("00"))){
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);

				jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用成功");
			}else{
				
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);

				jsonObject.put(ErrorMsg.KEY_MESSAGE,"-1");
				
			}
				
		}catch(Exception e){
			
			
			e.printStackTrace();
			
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);

			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用失败");
		}
		
		
		return this.writeJson(jsonObject);
	}

	/**
	 * 绑定备付金账户
	 * @return
	 */
	public String addUserAccount(){
		
		JSONObject jsonObject=new JSONObject();
		
		try{
			tsAccount.setOwnerId(this.getSessionUser().getShopId());
			tsAccount.setAccountTypeId("1");
			tsAccount.setOwnerTypeId("1");
			tsAccount.setBalance(tsAccount.getBalance()==null?Double.parseDouble("0.00"):tsAccount.getBalance());
			tsAccount.setPassword(tsAccount.getPassword());
			
			String shopId = this.getSessionUser().getShopId();
			formMap.put(TsAccount.COL_OWNER_ID, shopId);
			formMap.put(TsAccount.COL_OWNER_TYPE_ID, "1");
			SqlUtis.prepareSql(formMap, SqlUtis.getSQL(formMap, "a."
					+ TsAccount.COL_OWNER_ID, Types.VARCHAR, SqlUtis.EQ),
			SqlUtis.getSQL(formMap,"a."+TsAccount.COL_OWNER_TYPE_ID,Types.VARCHAR,SqlUtis.EQ));
			TsAccount account = this.accountService.one(formMap);
			
			if(account!=null && account.getStatusId().equals("05")){//激活
				jsonObject=accountPayService.activate("", tsAccount.getOwnerId(),tsAccount.getOwnerTypeId(),tsAccount.getPassword());
			}else{
				Map map = new HashMap();
				//		map.put("tsAccount", tsAccount);
				
				//		tsAccountService.insert(map);
				//shopsService.insertAccount(tsAccount);
				
				accountPayService.create(tsAccount.getOwnerId(), tsAccount.getOwnerTypeId(), tsAccount.getPassword(), tsAccount.getSignType(), tsAccount.getBalance()+"","");
				
				//this.saveActionMessage("绑定备付金账户成功！");
				//this.logService.addAdminAuditLog("", "绑定备付金账户成功,商家代码:"+tsAccount.getOwnerId());//记录操作日志
				
			}
			
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
	
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"新增支付密码成功");
		
		
		}catch(Exception e){
			
			
			e.printStackTrace();
			
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);

			jsonObject.put(ErrorMsg.KEY_MESSAGE,"调用失败");
		}
		
		
		return this.writeJson(jsonObject);
		
	}

}
